//
//  ConfirmCommonTicketViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-4.
//
//

#import "ConfirmCommonTicketViewController.h"
#import "NetworkAlertView.h"
#import <QuartzCore/QuartzCore.h>

@interface ConfirmCommonTicketViewController ()

@end

@implementation ConfirmCommonTicketViewController
{
    UIAlertView *_baseAlert;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //加载视图
    [self init_view];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//加载视图
- (void)init_view
{
    self.title = @"电子通票票";
    
    //加载图片
    UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+70, 280, 340)]autorelease];
    imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    imgView.image = [UIImage imageNamed:@"表格6行.png"];
    [self.view addSubview:imgView];

    //电影信息
    NSMutableArray *textarray = [[NSMutableArray alloc]init];
    [textarray addObject:[NSString stringWithFormat:@"影院：%@", self.cinemaName]];
    [textarray addObject:[NSString stringWithFormat:@"单价：%@元", [self.prices substringFromIndex:1]]];
    [textarray addObject:[NSString stringWithFormat:@"数量：%@张", self.count]];
    [textarray addObject:[NSString stringWithFormat:@"联系电话：13823642844"]];
    [textarray addObject:[NSString stringWithFormat:@"支付方式：网银支付"]];
    int allprice = [[self.prices substringFromIndex:1] intValue] * [self.count intValue];
    [textarray addObject:[NSString stringWithFormat:@"                                       合计：%d元", allprice]];
    int i;
    for (i = 0; i < [textarray count]; i ++) {
        UILabel *textlbl = [[[UILabel alloc]initWithFrame:CGRectMake(50, ADJUST_IOS7STATUES_HIGHT+125+30*i, 280, 30)]autorelease];
        textlbl.text = [textarray objectAtIndex:i];
        textlbl.font = [UIFont systemFontOfSize:15];
        textlbl.backgroundColor = [UIColor clearColor];
        textlbl.adjustsFontSizeToFitWidth = YES;
        [self.view addSubview:textlbl];
    }
    [textarray release];
    
    UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.frame = CGRectMake(40, ADJUST_IOS7STATUES_HIGHT+340, 230, 40);
    [nextStep setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [nextStep setTitle:@"确认支付" forState:UIControlStateNormal];
    nextStep.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    nextStep.layer.borderColor = [UIColor grayColor].CGColor;
    nextStep.layer.borderWidth = 1 ;
    nextStep.layer.cornerRadius = 10 ;
    [self.view addSubview:nextStep];
    [nextStep addTarget:self action:@selector(confirmMessge) forControlEvents:UIControlEventTouchUpInside];

}

- (void)confirmMessge
{
    NSInteger price = ([[self.prices substringFromIndex:1] intValue] * [self.count intValue]) * 100;
    //购买票务
    MovieManager *moviemanager = [MovieManager shareManager];
    moviemanager.delegate = self;
    [[NetworkAlertView shareView] showNetView];
    [moviemanager buyCommonTicket:self.cinemaName
                         cinemaId:self.cinemaId
                            count:self.count
                        userPhone:self.phone
                            price:[NSString stringWithFormat:@"%d", price]
                              tid:self.cinemaInfo.tID];
}

//购买通票
- (void)buyCommonTicket:(BOOL)isSuccess
                     tn:(NSString*)tn
{
    [[NetworkAlertView shareView] delView];
    if (isSuccess) {
        [UPPayPlugin startPay:tn sysProvide:nil spId:nil mode:PAY_MODE_REALEASE viewController:self delegate:self];
    } else {
        UIAlertView* baseAlert = [[[UIAlertView alloc] initWithTitle:@"下单失败" message:nil delegate:self cancelButtonTitle:@"下单失败，请重新尝试，谢谢" otherButtonTitles: nil] autorelease];
        [baseAlert show];
    }
}


- (void) performDismiss: (NSTimer *)timer {
    [_baseAlert dismissWithClickedButtonIndex:0 animated:NO];//important
    [_baseAlert release];
    _baseAlert = NULL;
}

-(void)UPPayPluginResult:(NSString*)result
{
    NSString *alertString = nil;
    if ([result isEqualToString:@"success"])
    {
        alertString = @"交易成功";
    }
    else if ([result isEqualToString:@"cancel"])
    {
        alertString = @"交易取消";
    }
    else
    {
        alertString = @"交易失败";
    }
    
    UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"支付结果" message:alertString  delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

@end
